using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

using PACKETPARSERMAX20.CORE;

namespace PACKETPARSERMAX20.WinUI
{
    public partial class frmPacketList : DockContent
    {
        public frmPacketList()
        {
            InitializeComponent();

            lstView.Columns[0].Width = 48;
            lstView.Columns[1].Width = 262;
            lstView.Columns[2].Width = 55;
            lstView.Columns[3].Width = 122;
            lstView.Columns[4].Width = 51;
            lstView.Columns[5].Width = 60;
            lstView.Columns[6].Width = 92;

            this.Width = 400;
        }

        private System.Collections.Hashtable mList = new System.Collections.Hashtable();
        public void Init()
        {
            StaticObjects.eventManager.EventObject_Updated += new EventManager.DelegateObject_Updated(Object_Updated);
        }

        private void Object_Updated(Object sender)
        {
            if (sender is Packet)
            {
                Packet tPacket = (Packet)sender;
                if (InvokeRequired)
                {
                    EventManager.DelegateObject_Updated tDelegate = new EventManager.DelegateObject_Updated(Object_Updated);
                    this.Invoke(tDelegate, new object[] { tPacket });
                }
                else
                {
                    if (lstView.Items.Count == 300)
                    {
                        lstView.Items[0].Remove();
                    }

                    ListViewItem lvItem = lstView.Items.Add(tPacket.Id.ToString());
                    lvItem.Tag = tPacket;
                    lvItem.SubItems.Add(string.Format("({0}){1}", tPacket.Opcode, tPacket.OpcodeName));
                    lvItem.SubItems.Add(tPacket.Direction.ToString());
                    lvItem.SubItems.Add(tPacket.CreatedDate.ToString());
                    lvItem.SubItems.Add(tPacket.Size.ToString());
                    if (tPacket.Result != null)
                        lvItem.SubItems.Add(tPacket.Result.ToString());
                    else
                        lvItem.SubItems.Add("");

                    if (tPacket.Warning != null)
                    {
                        lvItem.SubItems.Add(tPacket.Warning);
                        if (tPacket.Warning.Length != 0)
                        {
                            lvItem.ForeColor = Color.DarkOrange;
                        }
                    }
                    else
                        lvItem.SubItems.Add("");



                }
            }
        }

        public void Clear()
        {
        }

        public Packet SelectedItem
        {
            get
            {
                if (lstView.SelectedItems.Count > 0)
                {

                    return (Packet)lstView.SelectedItems[0].Tag;
                }
                return null;
            }
        }
        private void lstView_DoubleClick(object sender, EventArgs e)
        {
            Packet tSelectedItem = SelectedItem;
            if (tSelectedItem != null)
            {
                StaticObjects.eventManager.LoadHex(tSelectedItem.Data);
            }
            //PacketResult tResult = new PacketResult();
            //string test =  (string)tResult["test"];
            //tResult["test", 1] = "test";

            //foreach (ColumnHeader theader in lstView.Columns)
            //{
            //    System.Diagnostics.Debug.WriteLine("lstView.Columns[" + theader.Index.ToString() + "].Width = " + theader.Width.ToString());
            //}
        }

        private void mnuCopyOpcodeNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (SelectedItem != null)
            {
                Clipboard.Clear();
       
                Clipboard.SetText(SelectedItem.OpcodeName);

            }
        }

        private void mnuCopyOpcodeNumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (SelectedItem != null)
            {
                Clipboard.Clear();
                Clipboard.SetText(((int)SelectedItem.Opcode).ToString());
            }
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lstView.Items.Clear();
        }


    }
}
